<template>
  <el-dialog :title="!dataForm.id ? '新增' : '修改'" :close-on-click-modal="false" :visible.sync="visible" width="800px"
             top="8vh">
    <el-container>
      <el-main style="max-height:70vh">
        <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()"
                 label-width="120px">
          <el-form-item label="公式分类" prop="gsFl">
            <el-select v-model="dataForm.gsFl" placeholder="请选择公式分类" :disabled="dataForm.id>0">
              <el-option label="部门" value="部门"></el-option>
              <el-option label="个人" value="个人"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="公式名称" prop="gsMc">
            <el-input v-model="dataForm.gsMc" placeholder="公式名称" style="width: 90%"></el-input>
          </el-form-item>
          <el-form-item label="公式说明" prop="gsSm">
            <el-input type='textarea' v-model="dataForm.gsSm" placeholder="公式说明" rows="5" style="width: 90%">
            </el-input>
          </el-form-item>
        </el-form>
      </el-main>
    </el-container>
    <span slot="footer" class="dialog-footer">
      
      <el-button type="primary" @click="dataFormSubmit()" v-if="canSubmit">确定</el-button>
      <el-button @click="visible = false">取消</el-button>
    </span>
  </el-dialog>
</template>
<script>
    import {getObj, addObj, putObj} from '@/api/admin/sysformula'

    export default {
    data () {
      return {
        visible: false,
        canSubmit: false,
        dataForm: {
          id: 0,
          gsFl: '',
          gsMc: '',
          gsSm: '',
          gsPz: '',
          gsZs: '',
          gsYs: '',
          gsYz: '0',
          gsZt: '1',
          jjDeptId :0
        },
        fl:'',
        dataRule: {
          gsType:[{ required: true, message: '公式大类不能为空', trigger: 'blur' }],
          gsFl: [
            { required: true, message: '公式分类不能为空', trigger: 'blur' }
          ],
          gsMc: [
            { required: true, message: '公式名称不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id,deptId,fl) {
        this.dataForm.id = id || 0;
        if(fl){
         this.fl = fl;
        }
        this.visible = true;
        this.canSubmit = true;
        this.$nextTick(() => {
          // this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            getObj(this.dataForm.id).then(response => {
               //console.log(111)
                this.dataForm = response.data.data
            })
          }else {
            this.dataForm = {}
            this.dataForm.jjDeptId = deptId;
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.canSubmit = false;
            if (this.dataForm.id) {
                putObj(this.dataForm).then(data => {
                    this.$notify.success('修改成功')
                    this.visible = false
                    this.$emit('refreshDataList')
                }).catch(() => {
                    this.canSubmit = true;
                });
            } else {
                this.dataForm.gsType=this.fl;
                addObj(this.dataForm).then(data => {
                    this.$notify.success('添加成功')
                    this.visible = false
                    this.$emit('refreshDataList')
                }).catch(() => {
                    this.canSubmit = true;
                });
            }
          }
        })
      }
    }
  }
</script>
